package qdu.my.ch2_service;


import qdu.my.ch2_service.Bind_srv.Mybinder;
import qdu.my.ch2_service.MusicService.Music_binder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * ---------------------------------------
 *--请将你的笔记注释到程序末尾，否则行号会不准确--
 * ---------------------------------------
 *
 * 首先我们暂时规定: 发起服务请求的安卓组件称为“启动端”，被请求的服务组件称为“服务端”
 *
 * 安卓服务有两套生命周期模式，如课件程序中背景图片所示。
 * 分别为非绑定模式（line 65）和绑定模式（line 74）。
 */
public class My1st_service extends Activity{
	Intent srv_it, srv_it2;
	Intent music_srv1;
	Mybinder mb1; Music_binder mb2;
	Myconn mc = new Myconn();
	class Myconn implements ServiceConnection{
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mb1 = (Mybinder)service;
//			if(service != null){
//				((Mybinder)service).getservice();
//			}
		}
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}		
	}
	class Music_conn implements ServiceConnection{
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mb2 = (Music_binder)service;
		}
		@Override
		public void onServiceDisconnected(ComponentName name) {	}		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.my1stsrv_ly);
        
        ((ImageView)findViewById(R.id.imgv)).setImageResource(R.drawable.life_srv);

        findViewById(R.id.btn_nobind).setOnClickListener(new View.OnClickListener() {
			@SuppressLint("SuspiciousIndentation")
			@Override
			public void onClick(View v) {
				srv_it = new Intent(My1st_service.this, Nobind_srv.class);
			    startService(srv_it);
			}
		});

        findViewById(R.id.btn_bind).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				srv_it2 = new Intent(My1st_service.this, Bind_srv.class);
				bindService(srv_it2, mc, Context.BIND_AUTO_CREATE);

			}
		});
        //
        findViewById(R.id.btn_getsrv).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mb1.getservice();		
			}
		});
        

        music_srv1 = new Intent(this, MusicService.class);
        //bindService(music_srv1, new Music_conn(), Context.BIND_AUTO_CREATE);
        findViewById(R.id.iv_musicsrv).setOnClickListener(music_srv_listener);
      //*****************************styles*************************
        String send_code = this.getString(R.string.basic_qdu_steps);
        SpannableString spstr = new SpannableString(send_code);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#0099EE"));
        spstr.setSpan(colorSpan, spstr.length()-4, spstr.length()-2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spstr.setSpan(new StrikethroughSpan(), spstr.length()-4, spstr.length()-2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        TextView sendcode = (TextView)findViewById(R.id.tv_steps);
        sendcode.setText(spstr);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		stopService(srv_it);
		unbindService(mc);
		System.out.println("main activity destory");
	}
    //*********************Music service*********************
	View.OnClickListener music_srv_listener=new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			//MusicService
			if(MusicService.isplay==false){
				startService(music_srv1);//
				//mb2.startmusic();
				((ImageView)v).setImageDrawable(getResources().getDrawable(R.drawable.start,null));//
			}else{
				stopService(music_srv1);
				//mb2.stopmusic();
				((ImageView)v).setImageDrawable(getResources().getDrawable(R.drawable.pause, null));//
			}			
		}		
	};
}
